<template>
	<view class="my-record" ref="uv-back-top">
		<headers></headers>
		<el-container>
			<view class="hidden-sm-and-down" style="max-width:500px">
				<my :heads="false"></my>
			</view>
			<!--height: calc(100vh - 60px);overflow:auto :title="item.nickname" :note="item.msg"
			:thumb="item.portrait" thumb-size="lg" :rightText="ctime(item.createdAt)" -->
			<el-main style="padding: 0px;margin: 0px;">
				<view style="margin: 5px;border-radius: 5px;border: 1px solid #eee;">
					<uv-tabs :list="uvTabs" @click="click"></uv-tabs>
					<uv-list @scrolltolower="loadMore" class="record_content infinite-list" v-infinite-scroll="loadMore"
						:height="300">
						<uv-list-item v-for="(item,index) in list" :key="item.createdAt">
							<view class="record_list">
								<view>
									<view style="display: flex;">
										昵称：{{vuex_user.nickname}}
									</view>
									<view style="display: flex;">
										操作：{{ctype(item.taskType)}}-{{item.msg}}
									</view>
								</view>
								<view class="">
									<view v-if="item.value>0" style="color: blue;">
										{{item.value}}
									</view>
									<view v-else-if="item.value<0" style="color: #F56C6C;">
										{{item.value}}
									</view>
									<view v-else>
										{{item.value}}
									</view>
									<view class="">
										{{ctime(item.createdAt)}}
									</view>
								</view>
							</view>
						</uv-list-item>
					</uv-list>
					<uv-load-more v-if="list[0]" :status="status" :loading-text="loadingText"
						:loadmore-text="loadmoreText" :nomore-text="nomoreText" @loadmore="loadMore" />
					<el-empty v-else description="什么都没有!"></el-empty>
				</view>

			</el-main>
		</el-container>
		<uv-toast ref="toast"></uv-toast>
		<uv-navbar v-if="!screen" title="消费记录" @leftClick="navigateBack" rightText="" :placeholder="true"></uv-navbar>
		<uv-back-top :scroll-top="scrollTop"></uv-back-top>
		<!-- <tabbars :prop-value="'my'"></tabbars> -->
	</view>
</template>

<script>
	// 引入
	import config from '@/common/config'
	import tabbars from '@/components/tabbar/tabbar';
	import my from '@/pages/my/index'
	import headers from '@/components/header/header.vue';
	import {
		getRecords
	} from '@/common/api.js';
	export default {
		components: {
			// 注册组件
			tabbars,
			headers,
			my
		},
		data() {
			return {
				loading: false,
				pageSize: 15,
				page: 1,
				total: 0, //总条数
				hasMore: true, // 是否有更多数据
				status: 'nomore',

				scrollTop: 0,
				uvTabs: [{
					name: '全部',
					value: null,
				}, {
					name: '消耗',
					value: '消耗',
				}, {
					name: '充值',
					value: '充值',
				}, {
					name: '返还',
					value: '返还',
				}, {
					name: '其他',
					value: 'other',
				}],
				list: [],
				keyword: '', //搜索框
				type: '全部',
				noMore: false,
				loadingText: '正在加载...',
				loadmoreText: '加载更多',
				nomoreText: '没有更多了'
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			//重新加载
			this.page = 1;
			this.getData() //请求更多数据列表
			uni.stopPullDownRefresh(); //停止刷新
		},
		onReachBottom() {
			this.loadMore()
		},
		onPageScroll: function(e) {
			console.log("滚动距离为：" + e.scrollTop);
			if (this.hasMore) {
				this.getData(true); // 当触底时加载更多数据
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
			// #ifdef APP-NVUE
			this.scrollTop = e.detail.scrollTop;
			// #endif
		},
		computed: {
			disabled() {
				return this.loading || this.noMore
			}
		},
		mounted() {
			this.getData();
		},
		methods: {
			click(item) {
				// console.log('item', item);
				this.type = item.value;
				this.page = 1;
				this.getData();
			},
			ctype(type) {
				if (type === 'admin') {
					return '管理员'
				}
				if (type === 'chat') {
					return '语言'
				}
				if (type === 'audio-generation' || type === 'music') {
					return '音频'
				}
				if (type === 'image-recognition') {
					return '观止'
				}
				if (type === 'image-generation' || type === 'painting' || type === 'images' || type ===
					'generate' || type === 'fetch' || type ===
					'imagine') {
					return '成图'
				}
				if (type === 'generations' || type === 'video') {
					return '视频'
				}
				if (type === 'fusion') {
					return '聚合';
				}
				if (type === 'target') {
					return '翻译';
				}
				if (type === 'user') {
					return '用户';
				}
			},
			ctime(time) {
				return uni.$uv.date(new Date(time).getTime(), 'yyyy/mm/dd hh:MM:ss')
			},
			loadMore() {
				if (this.status != 'nomore') {
					this.status = 'loading';
					var allTotal = this.page * this.pageSize
					if (allTotal < this.total) {
						//this.total为请求数据的总条数。只要现有条数小于总条数就执行一下代码
						this.page++;
						this.getData(true) //请求更多数据列表
					} else {
						this.status = 'nomore';
						this.$refs.toast.show({
							type: 'error',
							title: '默认主题',
							message: "没有更多数据了"
						})
					}
				}

			},
			getData(ee = false) {
				var that = this;
				getRecords({
					custom: {
						auth: true,
						// toast: false,
						catch: true,
					},
					params: {
						pageSize: this.pageSize,
						page: this.page,
						type: this.type,
					}
				}).then(e => {
					if (ee) {
						this.list = this.list.concat(e.list);
					} else {
						this.list = e.list;
					}
					this.status = 'loadmore';
					this.page = e.page;
					this.pageSize = e.pageSize;
					this.total = e.total;
				}).catch(e => {
					console.log(e);
				})
			},
			routed(url) {
				uni.$uv.route({
					url: url,
					intercept(params, next) {
						console.log(params, next)
						uni.showLoading({
							title: '等待中...'
						})
						// 模拟处理等待
						setTimeout(() => {
							uni.hideLoading();
							next(true);
						}, 1000)
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.my-record {
		padding: 0px;
		width: 100%;

		// width: calc(100% - 20px);
		// padding: 10px;
		::v-deep .record_content {
			overflow: auto;
			padding: 0px;
			max-height: calc(100vh - 150px);

			.li_list {
				display: flex;
				// justify-content: space-between;
			}

			// background-color: #000;
			.record_list {
				display: flex;
				justify-content: space-between;
				align-items: center;
				border: 2px solid #eeeeee;
				margin: 10px 5px;
				padding: 10px;
				background-color: rgba(255, 255, 255, 0.1);
				border-radius: 10px;
			}
		}

		::v-deep .uv-list-item {
			background-color: #000;
		}
	}
</style>